#!/bin/sh
# Grid Engine options (lines prefixed with #$)
#$ -N runPedigreeTest
#$ -cwd                  
#$ -l h_rt=12:00:00
#$ -l h_vmem=4G
#$ -pe sharedmem 1
#$ -e logs-tests
#$ -o logs-tests

if [ $1 = submit ] ; then 
    module load  uge
    # Figure 1: HD Markers
    for nHD in 100 500 1000; do
        cp -r base/* data/hd_markers/dataFor-10-${nHD}-1

        for ntraits in 1 5 10 25 50 100; do 
            qsub runTest.sh hd_markers all 10 $nHD 1 0.5 $ntraits 0
        done
    done
    
    # Figure 1: Base set. Vary gvar + training
    cp -r base/* data/base/dataFor-10-500-1
    for gvar in 0.1 0.2 0.3 0.4 0.5 0.6 0.7; do
        for ntraits in 1 5 10 25 50 100; do 
            qsub runTest.sh base all 10 500 1    $gvar $ntraits 0
        done
    done

    for train in 100 250 500 750 1000; do 
        for ntraits in 1 5 10 25 50 100; do 
            qsub runTest.sh base all 10 500 1   0.5 $ntraits $train
        done
    done

    # Figure 1: nChr
    for nChr in 5 10 15 20; do
        cp -r base/* data/num_chr/dataFor-${nChr}-500-1

        for ntraits in 1 5 10 25 50 100; do 
            qsub runTest.sh num_chr all $nChr 500 1  0.5 $ntraits 0
        done
    done

    # Figure 1: map length
    for map in 0.5 1 2 4; do
        cp -r base/* data/map_len/dataFor-10-500-${map}
        for ntraits in 1 5 10 25 50 100; do 
            qsub runTest.sh map_len all 10 500 $map  0.5 $ntraits 0
        done
    done
    exit 1

fi

if [ $1 = test ] ; then 
    cp -r base/* data/base/dataFor-10-500-1
    ./runTest.sh base 1 10 500 1 1 30 0
    exit 1
fi

if [ $2 = all ] ; then 
    params="${@:3}"
    for rep in {1..10} ; do
        echo Now running $rep 
        ./runTest.sh $1 $rep $params
    done 
    exit 1
fi 

blockimpute=`pwd`/blockimpute/src/runBlockImpute.py
results_folder=`pwd`/results
. /etc/profile.d/modules.sh

. ~/.bashrc
export OMP_NUM_THREADS=$NSLOTS
export MKL_NUM_THREADS=$NSLOTS

sim=$1
rep=$2
nChr=$3
nSnp=$4
maplength=$5


gvar=$6
ntraits=$7
training=$8


fileDir=data/$sim/$rep/dataFor-$nChr-$nSnp-$maplength
condition=$gvar.$training.$ntraits

cd $fileDir

module load anaconda/4.3.1
source activate whalen36

### Create data
pedigree=pedigree.txt

folder=traits/gvar-$gvar/

dataFolder=data/$gvar/$ntraits/$training
mkdir -p $dataFolder

imputedir=imputed/$gvar/$ntraits
mkdir -p $imputedir

Rscript extractPhenotypesAndQtl.r $folder $ntraits $training $dataFolder

# ## Run Imputation

python $blockimpute -phasefile haplotypes.parents \
                -pedigree pedigree.txt \
                -out $imputedir/peel.$condition \
                -map map.txt -qtl $dataFolder/qtl.txt -phenotypes $dataFolder/phenotypes.children -haploid


### Analyze Data
results=results/results.$sim.$rep.$nChr.$nSnp.$maplength.$condition
rm $results.dosages
Rscript analyzeData.r $imputedir/peel.$condition.dosages $results
cp $results $results_folder


